(ns net.marigolda.test-so-start
  (:use clojure.test)
	(:use net.marigolda.so-start))

;;init data
(def arguments-map {:one "onevalue" :two "twovalue" :three "threevalue"})
(def arguments-map-result "&one=onevalue&two=twovalue&three=threevalue")


;;tests
(deftest test-format-filter-map
  (is (= arguments-map-result (format-filter-map arguments-map))))

(deftest test-format-filter-map-empty
  (is (= "" (format-filter-map {}))))
         
(deftest test-format-filter-map-2 
  (is (= arguments-map-result (format-filter-map-2 arguments-map))))

(deftest test-format-filter-map-2-empty
  (is (= "" (format-filter-map {}))))

(deftest test-my-div
  (is (= 0 (my-div 23 0)))
  ;;(is (not (thrown? java.lang.ArithmeticException (my-div 23 0))))
  (is (not (= 0 (my-div 23 234)))))
  
;;(deftest test-read-so-api 
;;  (let [clj-tags (read-so-api "/tags" {:filter "clojure"})]
;;    (is (:total clj-tags))
;;    (is (:tags clj-tags))))

;; note: record equality with other maps, = includes type, .equals doesn't
(deftest test-get-score
  (let [ans {:question_id 3262195, :score 2, :down_vote_count 0, :creation_date 1279296923, :answer_id 3266877, :owner {:user_id 23070, :user_type "registered", :display_name "Brian Carper", :reputation 17277, :email_hash "4d84ec3981443dfd9c287e845b60d2ce"}, :title "Compact Clojure code for regular expression matches and their position in string", :up_vote_count 2, :answer_comments_url "/answers/3266877/comments", :last_activity_date 1279296923, :view_count 87, :community_owned false, :accepted true}
        ques {:favorite_count 0, :question_timeline_url "/questions/3262195/timeline", :question_id 3262195, :question_comments_url "/questions/3262195/comments", :score 4, :down_vote_count 0, :creation_date 1279258636, :owner {:user_id 390868, :user_type "registered", :display_name "Gabriel Mitchell", :reputation 142, :email_hash "ad3aba527c3473350cc35471d9cb48f8"}, :title "Compact Clojure code for regular expression matches and their position in string", :up_vote_count 4, :question_answers_url "/questions/3262195/answers", :answer_count 1, :last_activity_date 1279296923, :view_count 87, :community_owned false, :answers [ans], :accepted_answer_id 3266877, :tags ["regex" "clojure"]}
        ans-score {:nof-qs 0, :nof-qs-view 0, :nof-qs-up 0, :nof-qs-down 0, :nof-qs-fav 0, :nof-as 1, :nof-as-view 87, :nof-as-up 2, :nof-as-down 0, :nof-as-accept 1}
        ques-score {:nof-qs 1, :nof-qs-view 87, :nof-qs-up 4, :nof-qs-down 0, :nof-qs-fav 0, :nof-as 0, :nof-as-view 0, :nof-as-up 0, :nof-as-down 0, :nof-as-accept 0}]
    (is (.equals ans-score (get-score ans)))
    (is (.equals ques-score (get-score ques)))))

(deftest test-make-user
  (let [post {:favorite_count 0, :question_timeline_url "/questions/3262195/timeline", :question_id 3262195, :question_comments_url "/questions/3262195/comments", :score 4, :down_vote_count 0, :creation_date 1279258636, :owner {:user_id 390868, :user_type "registered", :display_name "Gabriel Mitchell", :reputation 142, :email_hash "ad3aba527c3473350cc35471d9cb48f8"}, :title "Compact Clojure code for regular expression matches and their position in string", :up_vote_count 4, :question_answers_url "/questions/3262195/answers", :answer_count 1, :last_activity_date 1279296923, :view_count 87, :community_owned false, :answers [], :accepted_answer_id 3266877, :tags ["regex" "clojure"]}]
    (is (.equals {:user-id 390868, :dname "Gabriel Mitchell", :emailh "ad3aba527c3473350cc35471d9cb48f8", :nof-qs 0, :nof-qs-view 0, :nof-qs-up 0, :nof-qs-down 0, :nof-qs-fav 0, :nof-as 0, :nof-as-view 0, :nof-as-up 0, :nof-as-down 0, :nof-as-accept 0} (make-user post)))))

(deftest test-register-post
  (let [post {:favorite_count 0, :question_timeline_url "/questions/3262195/timeline", :question_id 3262195, :question_comments_url "/questions/3262195/comments", :score 4, :down_vote_count 0, :creation_date 1279258636, :owner {:user_id 390868, :user_type "registered", :display_name "Gabriel Mitchell", :reputation 142, :email_hash "ad3aba527c3473350cc35471d9cb48f8"}, :title "Compact Clojure code for regular expression matches and their position in string", :up_vote_count 4, :question_answers_url "/questions/3262195/answers", :answer_count 1, :last_activity_date 1279296923, :view_count 87, :community_owned false, :answers [], :accepted_answer_id 3266877, :tags ["regex" "clojure"]}
        user {:user-id 390868, :dname "Gabriel Mitchell", :emailh "ad3aba527c3473350cc35471d9cb48f8", :nof-qs 1, :nof-qs-view 87, :nof-qs-up 4, :nof-qs-down 0, :nof-qs-fav 0, :nof-as 0, :nof-as-view 0, :nof-as-up 0, :nof-as-down 0, :nof-as-accept 0}        
        users {}]
    (is (not (contains? users :390868)))
    (is (.equals user (:390868 (register-post users post))))))

(deftest test-register-post-already-registereds
  (let [post {:favorite_count 0, :question_timeline_url "/questions/3262195/timeline", :question_id 3262195, :question_comments_url "/questions/3262195/comments", :score 4, :down_vote_count 0, :creation_date 1279258636, :owner {:user_id 390868, :user_type "registered", :display_name "Gabriel Mitchell", :reputation 142, :email_hash "ad3aba527c3473350cc35471d9cb48f8"}, :title "Compact Clojure code for regular expression matches and their position in string", :up_vote_count 4, :question_answers_url "/questions/3262195/answers", :answer_count 1, :last_activity_date 1279296923, :view_count 87, :community_owned false, :answers [], :accepted_answer_id 3266877, :tags ["regex" "clojure"]}
        init-user {:user-id 390868, :dname "Gabriel Mitchell", :emailh "ad3aba527c3473350cc35471d9cb48f8", :nof-qs 1, :nof-qs-view 1, :nof-qs-up 1, :nof-qs-down 1, :nof-qs-fav 1, :nof-as 1, :nof-as-view 1, :nof-as-up 1, :nof-as-down 1, :nof-as-accept 1}
				users-with-user {:390868 init-user}        
				user-with-registered {:nof-qs 2, :nof-as-accept 1, :user-id 390868, :nof-qs-fav 1, :nof-as-up 1, :nof-qs-down 1, :nof-as-view 1, :nof-qs-view 88, :dname "Gabriel Mitchell", :nof-as-down 1, :nof-as 1, :nof-qs-up 5, :emailh "ad3aba527c3473350cc35471d9cb48f8"}]
   (is (.equals user-with-registered (:390868 (register-post users-with-user post))))))

